package code.example.base.exception.invoke;

import code.example.tool.LogFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *  反射异常：InvocationTargetException 中target属性指的是实际的异常
 */
public class InvocationTargetExceptionTest {

    public void test(){
        try {
            Method method = Printer.class.getMethod("print", new Class[]{});
            Object obj = Printer.class.newInstance();
            try {
                method.invoke(obj, new Object[]{});
                LogFactory.getLog().info("invoke success");
            }catch (InvocationTargetException e){
                Throwable throwable = e.getCause();
                LogFactory.getLog().info("invoke error");
                LogFactory.getLog().error(throwable);
            }
        }catch (Exception e){
            LogFactory.getLog().error(e);
        }
    }
    public static void main(String args []){
        new InvocationTargetExceptionTest().test();
    }
}
